/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.guide.service.MobileAppointmentService;
import com.jeeplus.modules.guide.service.MobileLoginService;
import com.jeeplus.modules.guide.task.Send;
import com.jeeplus.modules.guide.utils.HttpRequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 预约取号业务Controller
 * @author azj
 * @version 2021-5-15
 */
@RestController
@RequestMapping("/mobileAppointment")
@RequiredArgsConstructor
public class MobileAppointmentController extends BaseController {
	private final MobileAppointmentService mobileAppointmentService;
	private final MobileLoginService mobileLoginService;
	private final Send send;
	@Value("queueUrl")
	private String queueUrl;

	/**
	 * getAppointmentInfo 获取用户预约信息，是否有效
	 * idcard 身份证
	 * sysno 点位标识
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getAppointmentInfo")
	public AjaxJson getAppointmentInfo(String sysno,String cardid) {
		return mobileAppointmentService.getAppointmentInfo(sysno,cardid);
	}

	/**
	 * updateAppointmentInfo 获取用户预约信息，是否有效
	 * idcard 身份证
	 * sysno 点位标识
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="updateAppointmentInfo")
	public AjaxJson updateAppointmentInfo(String winNo,String waiNum,String number,String id) {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("YYY-MM-dd HH:mm:ss");//格式化时间显示
		mobileAppointmentService.updateAppointmentInfo(number,id,sdf.format(now));
		Map<String,String> m = mobileAppointmentService.getAppointmentInfoById(id);
		if(null != m){
			String openid = mobileLoginService.getOpenid(m.get("cardid"));
			if(StringUtils.isNotBlank(openid)){
				String typeName = m.get("businessname");
				typeName = typeName.replace("(预约)","");
				if(StringUtils.isBlank(waiNum)){
					waiNum = "0";
				}
				send.sendAppointmentInfo(openid,number,typeName,waiNum,m.get("areaName"),winNo,sdf.format(now));
			}
		}
		return AjaxJson.success("操作成功！");
	}

	/**
	 * getQueueType 获取排号机业务
	 * areaId 点位标识
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getQueueType")
	public AjaxJson getQueueType(String areaId) {
		String result = mobileAppointmentService.getQueueType();
		if(StringUtils.isNotBlank(result)){
			result = result.replace("areaIdParam",areaId);
			result = HttpRequestUtil.sendQueue(queueUrl,result);
			if(StringUtils.isNotBlank(result)){
				return AjaxJson.success("请求成功！").put("data",result);
			}else{
				return AjaxJson.error("获取排队叫号业务数据失败！areaId:"+areaId).put("code",400);
			}
		}else{
			return AjaxJson.error("获取参数错误！areaId:"+areaId).put("code",400);
		}
	}
}
